package learnjava.basic.other;

import java.io.RandomAccessFile;

public class TestRandomAccess {

    public static void main(String[] args) throws Exception{
        //读二进制文件
//        RandomAccessFile ras = new RandomAccessFile("C:\\Users\\wangc\\Desktop\\ADS1292DATA_002.bin","r");
//        ras.seek(16*2); //按字节移动
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");
//        System.out.print(Integer.toHexString(ras.readShort())+" ");

        //写文件
        RandomAccessFile ras = new RandomAccessFile("C:\\Users\\wangc\\Desktop\\ADS1292DATA_003.bin","rw");
        ras.seek(50); //默认从头开始覆盖，指定偏移后从偏移位置开始覆盖，支持任意位置（包括文件末尾后面的位置）
        ras.write("wangc1".getBytes());
        ras.writeInt(65);
        ras.write("wangc2".getBytes());
        ras.writeInt(66);
        ras.write("wangc3".getBytes());
        ras.writeInt(67);
        ras.write("wangc4".getBytes());
        ras.writeInt(68);

        //RandomAccessFile类可以使多个线程在文件不同的位置同时写
    }

}
